extends Node2D

@export var offset: Vector2 = Vector2(0, -36)
@export var duration: float = 1.5
@export var max_width: int = 160

var label: Label
var timer: Timer

func _ready() -> void:
	label = Label.new()
	add_child(label)
	label.visible = false
	label.position = offset
	label.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
	label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	label.custom_minimum_size = Vector2(max_width, 0)
	timer = Timer.new()
	add_child(timer)
	timer.one_shot = true
	timer.timeout.connect(_hide_label)
	var sm := get_parent().get_node_or_null("StateMachine")
	if sm:
		sm.state_changed.connect(_on_state_changed)

func show_message(text: String) -> void:
	label.text = text
	label.visible = true
	timer.wait_time = duration
	timer.start()

func _hide_label() -> void:
	label.visible = false

func _on_state_changed(name: String) -> void:
	show_message("切换状态" + name)
